// pages/addProject/addProject.js
var server = require('../../utils/server');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    date: '',
    time: '',
    startDateString: '',
    startTimeString: '00:00',
    selectUserList: [],
    selectUserAllList: [],
    selectUserIndex: null,
  },
  
  // 选择开拓者的方法
  bindchooseChangeUser(e){
    let index = e.detail.value
    this.setData({
      selectUserIndex: index
    })
  },
  // 查询出所有的开拓者
  getUserList() {
    let params = {
      userType: 2,
    }
    server.post("/app/user/getUserAppListByUserType", params).then(res => {
      if (res.key === 1000) {
        let selectUserList = []
        let selectUserAllList = []
        res.result.list.forEach(item => {
          if(item.isAssistant == 1){
            selectUserList.push(item.nickName)
            selectUserAllList.push(item)
          }
        })
        this.setData({
          selectUserList,
          selectUserAllList,
          selectUserIndex: null,
        })
      }
    });
  },
  // 获取当前日期
  getNowDate() {
    const date = new Date()
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const halfHour = '00'
    const halfMinute = '00'
    if(this.data.date){
      let a = this.data.date.split("-")
      if(a[0] == year && a[1] == month && a[2] == day){
        halfHour = new Date(date.setHours(hour, minute + 30)).getHours()
        halfMinute = new Date(date.setHours(hour, minute + 30)).getMinutes()
      }
    }
    this.setData({
      startDateString: `${year}-${month}-${day}`,
      startTimeString: `${halfHour}:${halfMinute}`,
    })
  },
  bindDateChange(e) {
    const { value: val } = e.detail;
    const year = new Date().getFullYear();
    const month = new Date().getMonth() + 1;
    const date = new Date().getDate();
    const year1 = new Date(val).getFullYear();
    const month1 = new Date(val).getMonth() + 1;
    const date1 = new Date(val).getDate();
    let showHour = "00";
    let showMin = "00";
    if (year === year1 && month === month1 && date === date1) {
      const hour = new Date().getHours();
      const min = new Date().getMinutes();
      const addHour = min + 30 >= 60 ? 1 : 0;
      const min1 = (min + 30) % 60;
      showMin = min1 < 10 ? `0${min1}` : min1.toString();
      let b = hour;
      if (addHour === 1) {
        b = hour + 1
      }
      showHour = b < 10 ? `0${b}` : b < 24 ? b.toString() : "00";
    }
    const time = `${showHour}:${showMin}`;
    this.setData({
      date: val,
      time,
      endTime: `${val} ${time}`,
      startTimeString: time,
    });
  },
  bindTimeChange(e){
    this.setData({
      time: e.detail.value
    })
  },

  formSubmit(e) {
    let value = e.detail.value
    if(!value.project){
      wx.showToast({
        title: '请输入项目名称',
        icon: 'none'
      })
      return
    }
    if(!value.brands){
      wx.showToast({
        title: '请输入品牌名称',
        icon: 'none'
      })
      return
    }
    if(!value.price){
      wx.showToast({
        title: '请输入价格',
        icon: 'none'
      })
      return
    }
    if(!value.referencePrice){
      wx.showToast({
        title: '请输入参考价格',
        icon: 'none'
      })
      return
    }
    if(!value.date){
      wx.showToast({
        title: '请选择日期',
        icon: 'none'
      })
      return
    }
    if(!value.time){
      wx.showToast({
        title: '请选择时间',
        icon: 'none'
      })
      return
    }
    if(this.data.userInfo && this.data.userInfo.userType == 2 && this.data.userInfo.isAssistant == 2 && !value.pushUser){
      wx.showToast({
        title: '请选择开拓者',
        icon: 'none'
      })
      return
    }
    let params = {
      projectName: value.project,
      brands: value.brands,
      totalPrice: value.price,
      referencePrice: value.referencePrice,
      chooseEndTime: new Date(value.date + " " + value.time).getTime()
    }
    if (this.data.userInfo && this.data.userInfo.userType == 2 && this.data.userInfo.isAssistant == 2){
      let chooseIndex = parseInt(value.pushUser)
      params.choosePushUserId = this.data.selectUserAllList[chooseIndex].id
    }
    server.post("/app/user/addAppOtherProject", params).then(res => {
      if (res.key === 1000) {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(()=>{
          wx.navigateBack()
        }, 2000)
      }
    });
  },

  formReset(e) {
    this.setData({
      date: '',
      time: '',
      selectUserIndex: null,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getNowDate();
    this.getUserList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})